package com.lancoo.cvas.controller;


import cn.hutool.core.collection.CollUtil;
import com.lancoo.cvas.aspect.LogAnnotation;
import com.lancoo.cvas.entity.*;
import com.lancoo.cvas.pojo.common.Deleted;
import com.lancoo.cvas.pojo.vo.*;
import com.lancoo.cvas.service.*;
import com.lancoo.cvas.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jdk.nashorn.internal.objects.NativeJava;
import org.springframework.web.bind.annotation.*;
import sun.text.CollatorUtilities;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "开课计划控制器")
@RestController
@RequestMapping("/api/v5.0.1/web/coursePlan")
public class CoursePlanController {

    @Resource
    private BasicRuleService basicRuleService;

    @Resource
    private RoomService roomService;


    @Resource
    private CourseService courseService;

    @Resource
    private SegmentService segmentService;
    @Resource
    private ClassInfoService classInfoService;

    @Resource
    private PlanTeachingService planTeachingService;
    @Resource
    private TeachingClassWeekService teachingClassWeekService;
    @Resource
    private ParameterSettingService parameterSettingService;

    @GetMapping("/getClassInfoByClass")
    @ApiOperation("获取班级上课范围")
    @LogAnnotation(operationType = "3", operationContent = "获取班级上课范围")
    public Result<ClassInfoByClassResponses> getClassInfoByClass(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                 @ApiParam(value = "年级id ", required = false) @RequestParam(required = false) String gradeId,
                                                                 @ApiParam(value = "班级名模糊查询", required = false) @RequestParam(required = false) String keyWord,
                                                                 @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                                 @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize) {
        return Result.ofSuccess(basicRuleService.getClassInfoByClass(taskId, gradeId, keyWord, currentPage, pageSize));
    }

    @GetMapping("/getClassInfoByMajor")
    @ApiOperation("获取专业设置上课范围")
    @LogAnnotation(operationType = "3", operationContent = "获取专业设置上课范围")
    public Result<ClassInfoByMajorResponse> getClassInfoByMajor(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                @ApiParam(value = "基础平台专业id ", required = false) @RequestParam(required = false) String majorId,
                                                                @ApiParam(value = "年级id ", required = false) @RequestParam(required = false) String gradeId,
                                                                @ApiParam(value = "专业名模糊查询", required = false) @RequestParam(required = false) String keyWord,
                                                                @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                                @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize) {
        return Result.ofSuccess(basicRuleService.getClassInfoByMajor(taskId, majorId, gradeId, keyWord, currentPage, pageSize));
    }

    @GetMapping("/getOtherRoomListV2")
    @ApiOperation("获取未被选择的教室")
    @LogAnnotation(operationType = "3", operationContent = "获取未被选择的教室")
    public Result<List<RoomDto>> getOtherRoomListV2(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                    @ApiParam(value = "学校id ", required = false) @RequestParam String schoolId,
                                                    @ApiParam(value = "教室类型 ", required = false) @RequestParam(required = false) Integer roomType,
                                                    @ApiParam(value = "校区id", required = true) @RequestParam String campusId,
                                                    @ApiParam(value = "学期", required = true) @RequestParam String term) {
        return Result.ofSuccess(roomService.getOtherRoomListV2(taskId, schoolId, roomType, campusId, term));
    }

    @GetMapping("/getOtherRoomList")
    @ApiOperation("获取未被选择的教室")
    @LogAnnotation(operationType = "3", operationContent = "获取未被选择的教室")
    public Result<List<Room>> getOtherRoomList(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                               @ApiParam(value = "学校id ", required = false) @RequestParam String schoolId,
                                               @ApiParam(value = "教室类型 ", required = false) @RequestParam(required = false) Integer roomType,
                                               @ApiParam(value = "校区id", required = true) @RequestParam String campusId,
                                               @ApiParam(value = "学期", required = true) @RequestParam String term) {
        return Result.ofSuccess(roomService.getOtherRoomList(taskId, schoolId, roomType, campusId, term));
    }


    @GetMapping("/getAutoDistributionsRoom")
    @ApiOperation("自动分配教室")
    @LogAnnotation(operationType = "2", operationContent = "自动分配教室")
    public Result getAutoDistributionsRoom(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        return roomService.getAutoDistributionsRoom(taskId);
    }


    @GetMapping("/cleanDistributionsRoom")
    @ApiOperation("清空分配的教室")
    @LogAnnotation(operationType = "2", operationContent = "清空分配的教室")
    public Result cleanDistributionsRoom(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        return Result.ofSuccess(roomService.cleanDistributionsRoom(taskId));
    }

    @GetMapping("/setRoomV2")
    @ApiOperation("设置班级上课教室")
    @LogAnnotation(operationType = "2", operationContent = "设置班级上课教室")
    public Result<String> setRoomV2(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                    @ApiParam(value = "班级id ", required = true) @RequestParam String classId,
                                    @ApiParam(value = "教室id ", required = true) @RequestParam Long roomId,
                                    @ApiParam(value = "学校id", required = true) @RequestParam String schoolId) {
        return Result.ofSuccess(roomService.setRoomV2(taskId, classId, roomId, schoolId));
    }

    @GetMapping("/setRoom")
    @ApiOperation("设置班级上课教室")
    @LogAnnotation(operationType = "2", operationContent = "设置班级上课教室")
    public Result<String> setRoom(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                  @ApiParam(value = "班级id ", required = true) @RequestParam String classId,
                                  @ApiParam(value = "教室id ", required = true) @RequestParam Long roomId,
                                  @ApiParam(value = "学校id", required = true) @RequestParam String schoolId) {
        return Result.ofSuccess(roomService.setRoom(taskId, classId, roomId, schoolId));
    }

    @GetMapping("/getCourseListByClass")
    @ApiOperation("根据班级获取课程列表")
    @LogAnnotation(operationType = "3", operationContent = "根据班级获取课程列表")
    public Result<List<Course>> getCourseListByClass(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                     @ApiParam(value = "班级id ", required = true) @RequestParam(required = false) String classId,
                                                     @ApiParam(value = "班级id ", required = true) @RequestParam(required = false) String baseTeachingPlanId) {
        return Result.ofSuccess(courseService.getCourseListByClass(taskId, classId, baseTeachingPlanId));
    }


    @GetMapping("/checkRoom")
    @ApiOperation("检测是否有班级设置过这个教室")
    @LogAnnotation(operationType = "3", operationContent = "检测是否有班级设置过这个教室")
    public Result<String> checkRoom(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                    @ApiParam(value = "班级id ", required = true) @RequestParam(required = false) Long roomId) {
        return Result.ofSuccess(courseService.checkRoom(taskId, roomId));
    }


    @GetMapping("/getSegmentListByClass")
    @ApiOperation("根据班级获取环节列表")
    @LogAnnotation(operationType = "3", operationContent = "根据班级获取环节列表")
    public Result<List<Segment>> getSegmentListByClass(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                       @ApiParam(value = "班级id ", required = true) @RequestParam(required = true) String classId,
                                                       @ApiParam(value = "基础平台教学计划id ", required = true) @RequestParam(required = true) String baseTeachingPlanId) {
        return Result.ofSuccess(segmentService.getSegmentListByClass(taskId, classId, baseTeachingPlanId));
    }


    @GetMapping("/getCourseListByMajor")
    @ApiOperation("根据年级获取课程列表")
    @LogAnnotation(operationType = "3", operationContent = "根据年级获取课程列表")
    public Result<List<Course>> getCourseListByMajor(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                     @ApiParam(value = "年级id", required = true) @RequestParam(required = true) String gradeId,
                                                     @ApiParam(value = "专业id", required = true) @RequestParam(required = true) Long majorId,


                                                     @ApiParam(value = "专业id", required = true) @RequestParam(required = true) String baseTeachingPlanId) {
        return Result.ofSuccess(courseService.getCourseListByMajor(taskId, gradeId, majorId, baseTeachingPlanId));
    }


    @GetMapping("/getSegmentListByMajor")
    @ApiOperation("根据年级获取环节列表")
    @LogAnnotation(operationType = "3", operationContent = "根据年级获取环节列表")
    public Result<List<Segment>> getSegmentListByMajor(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                       @ApiParam(value = "年级id", required = true) @RequestParam(required = true) String gradeId,
                                                       @ApiParam(value = "专业id", required = true) @RequestParam(required = true) Long majorId,
                                                       @ApiParam(value = "专业id", required = true) @RequestParam(required = true) String baseTeachingPlanId) {
        return Result.ofSuccess(segmentService.getSegmentListByMajor(taskId, gradeId, majorId,baseTeachingPlanId));
    }


    @GetMapping("/getPlanTeachingByClass")
    @ApiOperation("根据班级获取教学计划列表")
    @LogAnnotation(operationType = "3", operationContent = "根据班级获取教学计划列表")
    public Result<List<PlanTeaching>> getPlanTeachingByClass(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                             @ApiParam(value = "班级id", required = true) @RequestParam(required = true) String classId) {
        return Result.ofSuccess(planTeachingService.getPlanTeachingByClass(taskId, classId));
    }


    @GetMapping("/getPlanTeachingByMajor")
    @ApiOperation("根据专业获取教学计划列表")
    @LogAnnotation(operationType = "3", operationContent = "根据专业获取教学计划列表")
    public Result<List<PlanTeaching>> getPlanTeachingByMajor(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                             @ApiParam(value = "年级id", required = true) @RequestParam(required = true) String gradeId,
                                                             @ApiParam(value = "专业id", required = true) @RequestParam(required = true) Long majorId) {
        return Result.ofSuccess(planTeachingService.getPlanTeachingByMajor(taskId, gradeId, majorId));
    }


    @GetMapping("/getNotEnableCourseList")
    @ApiOperation("获取没有启用的课程列表")
    @LogAnnotation(operationType = "3", operationContent = "获取班级没有启用的课程列表")
    public Result<List<AddCourseSegment>> getNotEnableCourseList(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                 @ApiParam(value = "课程类型id ", required = false) @RequestParam(required = false) String courseTypeId,
                                                                 @ApiParam(value = "课程性质id ", required = false) @RequestParam(required = false) Integer courseNature,
                                                                 @ApiParam(value = "基础平台教学计划id", required = false) @RequestParam(required = false) String baseTeachingPlanId,
                                                                 @ApiParam(value = "班级id", required = false) @RequestParam(required = false) String classId,
                                                                 @ApiParam(value = "课程名称关键字", required = false) @RequestParam(required = false) String keyWord) {
        return Result.ofSuccess(courseService.getNotEnableCourseList(taskId, baseTeachingPlanId, courseTypeId, classId, courseNature, keyWord));
    }


    @GetMapping("/getNotEnableCourseListByMajor")
    @ApiOperation("获取没有启用的课程列表")
    @LogAnnotation(operationType = "3", operationContent = "获取专业没有启用的课程列表")
    public Result<List<AddCourseSegment>> getNotEnableCourseListByMajor(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                        @ApiParam(value = "课程类型id ", required = false) @RequestParam(required = false) String courseTypeId,
                                                                        @ApiParam(value = "课程性质id ", required = false) @RequestParam(required = false) Integer courseNature,
                                                                        @ApiParam(value = "基础平台教学计划id", required = true) @RequestParam(required = false) String baseTeachingPlanId,
                                                                        @ApiParam(value = "年级id", required = true) @RequestParam(required = true) String gradeId,
                                                                        @ApiParam(value = "专业id", required = true) @RequestParam(required = true) Long majorId,
                                                                        @ApiParam(value = "课程名称关键字", required = true) @RequestParam(required = false) String keyWord) {
        return Result.ofSuccess(courseService.getNotEnableCourseListByMajor(taskId, baseTeachingPlanId, courseTypeId, gradeId, majorId, courseNature, keyWord));
    }


    @GetMapping("/getCourseType")
    @ApiOperation("获取课程类别")
    @LogAnnotation(operationType = "3", operationContent = "获取课程类别")
    public Result<List<CourseTypeDto>> getCourseType(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                     @ApiParam(value = "flag标志位 1课程 2环节", required = true) @RequestParam Integer flag) {
        return Result.ofSuccess(courseService.getCourseType(taskId, flag));
    }


    @GetMapping("/getNotEnableSegmentList")
    @ApiOperation("获取没有启用的环节列表")
    @LogAnnotation(operationType = "3", operationContent = "获取班级没有启用的环节列表")
    public Result<List<AddCourseSegment>> getNotEnableSegmentList(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                  @ApiParam(value = "环节类型id ", required = true) @RequestParam(required = false) String segmentTypeId,
                                                                  @ApiParam(value = "基础平台教学计划id", required = true) @RequestParam(required = false) String baseTeachingPlanId,
                                                                  @ApiParam(value = "班级id", required = true) @RequestParam(required = false) String classId,
                                                                  @ApiParam(value = "课程名称关键字", required = true) @RequestParam(required = false) String keyWord) {
        return Result.ofSuccess(courseService.getNotEnableSegmentList(taskId, baseTeachingPlanId, classId, segmentTypeId, keyWord));
    }

    @GetMapping("/getNotEnableSegmentListByMajor")
    @ApiOperation("获取没有启用的环节列表")
    @LogAnnotation(operationType = "3", operationContent = "获取专业没有启用的环节列表")
    public Result<List<AddCourseSegment>> getNotEnableSegmentListByMajor(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                         @ApiParam(value = "环节类型id ", required = true) @RequestParam(required = false) String segmentTypeId,
                                                                         @ApiParam(value = "基础平台教学计划id", required = true) @RequestParam(required = false) String baseTeachingPlanId,
                                                                         @ApiParam(value = "年级id", required = true) @RequestParam(required = true) String gradeId,
                                                                         @ApiParam(value = "专业id", required = true) @RequestParam(required = true) Long majorId,
                                                                         @ApiParam(value = "课程名称关键字", required = true) @RequestParam(required = false) String keyWord) {
        return Result.ofSuccess(courseService.getNotEnableSegmentListByMajor(taskId, baseTeachingPlanId, gradeId, majorId, segmentTypeId, keyWord));
    }


    @GetMapping("/courseAdd")
    @ApiOperation("教学计划增加(课程增开)")
    @LogAnnotation(operationType = "2", operationContent = "班级教学计划增加(课程增开)")
    public Result<String> courseAdd(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                    @ApiParam(value = "基础平台教学计划id", required = true) @RequestParam(required = true) String baseTeachingPlanId,
                                    @ApiParam(value = "课程id", required = true) @RequestParam String courseId,
                                    @ApiParam(value = "班级id", required = true) @RequestParam String classId,
                                    @ApiParam(value = "说明", required = true) @RequestParam String reserve,
                                    @ApiParam(value = "flag标志为1课程2环节", required = true) @RequestParam Integer flag) {
        courseService.courseAdd(taskId, baseTeachingPlanId, classId, courseId, flag, reserve);
        return Result.ofSuccess("课程已添加");
    }

    @GetMapping("/courseAddByMajor")
    @ApiOperation("教学计划增加(课程增开)")
    @LogAnnotation(operationType = "2", operationContent = "专业教学计划增加(课程增开)")
    public Result<String> courseAddByMajor(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                           @ApiParam(value = "基础平台教学计划id", required = true) @RequestParam(required = true) String baseTeachingPlanId,
                                           @ApiParam(value = "课程id", required = true) @RequestParam String courseId,
                                           @ApiParam(value = "年级id", required = true) @RequestParam(required = true) String gradeId,
                                           @ApiParam(value = "专业id", required = true) @RequestParam(required = true) Long majorId,
                                           @ApiParam(value = "说明", required = true) @RequestParam String reserve,
                                           @ApiParam(value = "flag标志为1课程2环节", required = true) @RequestParam Integer flag) {
        courseService.courseAddByMajor(taskId, baseTeachingPlanId, gradeId, majorId, courseId, flag, reserve);
        return Result.ofSuccess("课程已添加");
    }


    @GetMapping("/segmentAdd")
    @ApiOperation("教学计划增加（环节增开）")
    @LogAnnotation(operationType = "2", operationContent = "班级教学计划增加(环节增开)")
    public Result<String> segmentAdd(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                     @ApiParam(value = "基础平台教学计划id", required = true) @RequestParam(required = true) String baseTeachingPlanId,
                                     @ApiParam(value = "课程id", required = true) @RequestParam String segmentId,
                                     @ApiParam(value = "班级id", required = true) @RequestParam String classId,
                                     @ApiParam(value = "说明", required = true) @RequestParam String reserve,
                                     @ApiParam(value = "flag标志为1课程2环节", required = true) @RequestParam Integer flag) {
        courseService.segmentAdd(taskId, baseTeachingPlanId, segmentId, classId, flag, reserve);
        return Result.ofSuccess("环节已添加");
    }

    @GetMapping("/segmentAddByMajor")
    @ApiOperation("教学计划增加（环节增开）")
    @LogAnnotation(operationType = "2", operationContent = "专业教学计划增加(环节增开)")
    public Result<String> segmentAddByMajor(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                            @ApiParam(value = "基础平台教学计划id", required = true) @RequestParam(required = true) String baseTeachingPlanId,
                                            @ApiParam(value = "课程id", required = true) @RequestParam String segmentId,
                                            @ApiParam(value = "年级id", required = true) @RequestParam(required = true) String gradeId,
                                            @ApiParam(value = "专业id", required = true) @RequestParam(required = true) Long majorId,
                                            @ApiParam(value = "说明", required = true) @RequestParam String reserve,
                                            @ApiParam(value = "flag标志为1课程2环节", required = true) @RequestParam Integer flag) {
        courseService.segmentAddByMajor(taskId, baseTeachingPlanId, segmentId, gradeId, majorId, flag, reserve);
        return Result.ofSuccess("环节已添加");
    }


    @PostMapping("/closeCourseListByClass")
    @ApiOperation("关闭选定课程")
    @LogAnnotation(operationType = "2", operationContent = "班级关闭选定课程")
    public Result<Integer> closeCourseListByClass(@ApiParam(value = "关闭课程传参", required = true) @RequestBody(required = true) CloseCourseDTO closeCourseDTO) {

        return Result.ofSuccess(courseService.closeCourseListByClass(closeCourseDTO));

    }

    @PostMapping("/closeCourseListByMajor")
    @ApiOperation("关闭选定课程")
    @LogAnnotation(operationType = "2", operationContent = "专业关闭选定课程")
    public Result<Integer> closeCourseListByMajor(@ApiParam(value = "关闭课程传参", required = true) @RequestBody(required = true) CloseCourseDTOByMajor closeCourseDTO) {

        return Result.ofSuccess(courseService.closeCourseListByMajor(closeCourseDTO));

    }


    @PostMapping("/closeSegmentListByClass")
    @ApiOperation("关闭选定环节")
    @LogAnnotation(operationType = "2", operationContent = "班级关闭选定课程")
    public Result<Integer> closeSegmentListByClass(@ApiParam(value = "选中的环节ids", required = true) @RequestBody CloseSegmentDTO closeSegmentDTO) {

        return Result.ofSuccess(courseService.closeSegmentListByClass(closeSegmentDTO));
    }

    @PostMapping("/closeSegmentListByMajor")
    @ApiOperation("关闭选定环节")
    @LogAnnotation(operationType = "2", operationContent = "专业关闭选定课程")
    public Result<Integer> closeSegmentListByMajor(@ApiParam(value = "选中的环节ids", required = true) @RequestBody CloseSegmentDTOByMajor closeSegmentDTO) {

        return Result.ofSuccess(courseService.closeSegmentListByMajor(closeSegmentDTO));
    }


    @GetMapping("/getUnSetClassInfo")
    @ApiOperation("获取未设置教室的行政班")
    @LogAnnotation(operationType = "3", operationContent = "获取未设置教室的行政班")
    public Result<List<ClassInfo>> getUnSetClassInfo(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {

        return Result.ofSuccess(classInfoService.getUnSetClassInfo(taskId));
    }


    @GetMapping("/getUnSetTeachingMethods")
    @ApiOperation("获取未设置的授课方式")
    @LogAnnotation(operationType = "3", operationContent = "获取未设置的授课方式")
    public Result<List<Integer>> getUnSetTeachingMethods(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                         @ApiParam(value = "任务流程id", required = true) @RequestParam Long teachingClassId) {

        //获取对应教学班未设置授课方式
        List<TeachingClassWeek> list = teachingClassWeekService.lambdaQuery().eq(TeachingClassWeek::getTeachingClassId, teachingClassId).eq(TeachingClassWeek::getDeleted, Deleted.NOT.getCode()).list();
        List<Integer> collect = list.stream().map(TeachingClassWeek::getHourType).collect(Collectors.toList());
        List<Integer> res = new ArrayList<>(Arrays.asList(1, 2, 3, 4));
        if (CollUtil.isNotEmpty(collect)) {
            res.removeAll(collect);
        }
        return Result.ofSuccess(res);
    }


    @GetMapping("/getUnSetTeachingMethodsByParameter")
    @ApiOperation("获取未设置的授课方式")
    @LogAnnotation(operationType = "3", operationContent = "获取未设置的授课方式")
    public Result<List<Integer>> getUnSetTeachingMethodsByParameter(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                    @ApiParam(value = "年级id", required = true) @RequestParam String gradeId,
                                                                    @ApiParam(value = "课程id", required = true) @RequestParam Long courseId) {

        //获取对应班级未设置授课方式
        List<ParameterSetting> list = parameterSettingService.lambdaQuery().eq(ParameterSetting::getGradeId, gradeId).eq(ParameterSetting::getCourseId, courseId)
                .eq(ParameterSetting::getDeleted, Deleted.NOT.getCode()).list();
        List<Integer> collect = list.stream().map(ParameterSetting::getHourType).collect(Collectors.toList());
        List<Integer> res = new ArrayList<>(Arrays.asList(1, 2, 3, 4));
        if (CollUtil.isNotEmpty(collect)) {
            res.removeAll(collect);
        }
        return Result.ofSuccess(res);
    }


}
